package com.renchunlin.slidemenuactivity.view

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.renchunlin.slidemenuactivity.R

/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/7/9.
 * PS: Not easy to write code, please indicate.
 */
class SlideMenuView : ViewGroup, View.OnClickListener {

    companion object {
        const val TAG = "SlideMenuView"
    }

    private var mFunction = -1
    private lateinit var mContentView: View
    private lateinit var mEditView: View
    private lateinit var onEditClickListener: OnEditClickListener
    private lateinit var readTv: TextView
    private lateinit var topTv: TextView
    private lateinit var deleteTv: TextView

    //private var contentLeft = 0
    private var downX = 0f

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        val a =
            context.obtainStyledAttributes(attrs, R.styleable.SlideMenuView)
        mFunction = a.getInt(R.styleable.SlideMenuView_function, 0x30)
        a.recycle()
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        val childCount = childCount
        //加个判断，只能有一个子view
        if (childCount > 1) {
            throw IllegalArgumentException("no more then one child.")
        }
        mContentView = getChildAt(0)
        //根据属性，继续添加子view
        mEditView = LayoutInflater.from(context).inflate(R.layout.item_slide_action, this, false)
        initEditView()
        this.addView(mEditView)
        mEditView
    }

    private fun initEditView() {
        readTv = mEditView.findViewById(R.id.read_tv)
        topTv = mEditView.findViewById(R.id.top_tv)
        deleteTv = mEditView.findViewById(R.id.delete_tv)

        /*readTv.setOnClickListener(this)
        topTv.setOnClickListener(this)
        deleteTv.setOnClickListener(this)*/
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        //测量第一个孩子，也就是内容部分
        //宽度，跟父控件一样宽。高度有三种情况，如果指定的大小，那我们获取到它的大小，直接测量
        //如果是包含内容，at_most,如果是match_parent,那就给它大小。
        val contentLayoutParams = mContentView.layoutParams
        val contentHeightMeasureSpec: Int = when (val contentHeight = contentLayoutParams.height) {
            LayoutParams.MATCH_PARENT -> {
                MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY)
            }
            LayoutParams.WRAP_CONTENT -> {
                MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST)
            }
            else -> {
                //指定大小
                MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY)
            }
        }
        mContentView.measure(widthMeasureSpec, contentHeightMeasureSpec)
        //拿到内容部分测量以后的高度
        val contentMeasureHeight = mContentView.measuredHeight
        //测量编辑部份，宽度：3/4，高度跟内容高度一样
        val editWidthSize = widthSize * 3 / 4
        mEditView.measure(
            MeasureSpec.makeMeasureSpec(editWidthSize, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(contentMeasureHeight, MeasureSpec.EXACTLY)
        )
        //测量自己
        //宽度就是前面宽度的总和，高度和内容一样高
        setMeasuredDimension(widthSize + editWidthSize, contentMeasureHeight)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        //摆放内容
        val contentLeft = 0
        val contentTop = 0
        val contentRight = contentLeft + mContentView.measuredWidth
        val contentBottom = contentTop + mContentView.measuredHeight
        mContentView.layout(contentLeft, contentTop, contentRight, contentBottom)
        //摆放编辑部份
        val editViewLeft = contentRight
        val editViewTop = contentTop
        val editViewRight = editViewLeft + mEditView.measuredWidth
        val editViewBottom = editViewTop + mEditView.measuredHeight
        mEditView.layout(editViewLeft, editViewTop, editViewRight, editViewBottom)
    }

    fun setOnEditClickListener(onEditClickListener: OnEditClickListener) {
        this.onEditClickListener = onEditClickListener
    }

    //动作的监听
    interface OnEditClickListener {
        fun onReadClick()
        fun onTopClick()
        fun onDeleteClick()
    }

    override fun onClick(v: View?) {
        when (v) {
            readTv -> {
                onEditClickListener.onReadClick()
            }

            topTv -> {
                onEditClickListener.onTopClick()
            }

            deleteTv -> {
                onEditClickListener.onDeleteClick()
            }
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                Log.i("chen", "ACTION_DOWN: ")
                downX = event.x
                val y = event.y
                Log.i("chen", "downX: $downX")
                //Log.i("chen", "downy: $y")
            }
            MotionEvent.ACTION_UP -> {
                Log.i("chen", "ACTION_UP: ")
                val x = event.x
                val y = event.y
                Log.i("chen", "upX: $x")
                //Log.i("chen", "upY: $y")
            }
            MotionEvent.ACTION_MOVE -> {
                //已经滑动的值
                val scrollX = scrollX
                
                Log.i("chen", "ACTION_MOVE: ")
                val moveX = event.x
                val y = event.y
                Log.i("chen", "moveX: $moveX")
                //Log.i("chen", "moveY: $y")

                //移动的差值
                val dx = moveX - downX
                //判断边界
                //把差值使用起来
                Log.i("chen", "差值: $dx")
                //contentLeft += dx.toInt()
                scrollBy(-dx.toInt(), 0)
                //重复布局
                //requestLayout()
            }
        }
        return true
    }
}